package com.khh.servlet;


import com.khh.dao.BookDao;
import com.khh.entity.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by webrx on 2017-08-26.
 */

@WebServlet("/bookservlet")
public class Bookservlet extends HttpServlet{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        BookDao bookDaos = new BookDao();
        //List<Book> boks = bookDaos.queryall();
       /* for (Book b:boks ) {
            System.out.println(b);
        }*/
       String p = req.getParameter("p");
        int currpage = p==null?1:Integer.parseInt(p);
        int size = 3;
        int recordcount = bookDaos.getCount();
        int pagecount = recordcount%size==0 ? recordcount/size : recordcount/size+1;
        if(currpage<1) currpage=1;
        if(currpage>pagecount) currpage = pagecount;

        int start = currpage*size-size;
        List<Book> p1 = bookDaos.page(start,size);
        req.setAttribute("bookss",p1);
        req.setAttribute("pagen",currpage);
        req.setAttribute("pagecount",pagecount);
        req.getRequestDispatcher("show.jsp").forward(req,resp);
    }
}
